package com.bluedon.ism.commons.entity;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import static javax.persistence.GenerationType.IDENTITY;

/**
 * 用户账号表
 */
@Entity
@Table(name = "ACCOUNT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Account implements java.io.Serializable {
	
	private static final long serialVersionUID = 1974442093837483653L;
	private Long aid;
	private Organization organization;
	/**
	 * 登录名 唯一标识
	 */
	private String username;
	
	/**
	 * 姓名
	 */
	private String realname;
	
	private String password;
	private Integer type;
	private Date createTime;
	private Account createUser;
	private Date lastSigninTime;
	private Date loginTime;
	private String lastSigninIp;
	private String loginIp;
	private Date modifiedTime;
	private Date resetTime;
	private String resetToken;
	private String email;
	private String mobile;
	private Long modifiedId;
	private Boolean disabled;
	private String numbers;
	private String dutyCode;//职务代码
	
	/**
	 * 1-系统内置，不可删除。 0-用户创建，可以删除
	 */
	private Boolean isSystem;
	private Integer countLogin;
	private Set<AccountRole> accountRoles = new HashSet<AccountRole>(0);


    @Id
    @GeneratedValue(strategy = IDENTITY)
	@Column(name = "AID", unique = true, nullable = false, precision = 10, scale = 0)
	public Long getAid() {
		return this.aid;
	}

	public void setAid(Long aid) {
		this.aid = aid;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORG_ID")
	public Organization getOrganization() {
		return this.organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	@Column(name = "USERNAME", nullable = false, length = 30, unique = true)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "REALNAME", nullable = false, length = 100)	
	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	@Column(name = "PASSWORD", nullable = false, length = 32)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "TYPE", precision = 1, scale = 0)
	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(name = "CREATE_TIME")
	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "CREATE_USER")
	public Account getCreateUser() {
		return createUser;
	}
	public void setCreateUser(Account createUser) {
		this.createUser = createUser;
	}
	
    @Column(name = "LOGIN_TIME")
    public Date getLoginTime() {
        return loginTime;
    }
    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }
	
	@Column(name = "LAST_SIGNIN_TIME")
	public Date getLastSigninTime() {
		return this.lastSigninTime;
	}

	public void setLastSigninTime(Date lastSigninTime) {
		this.lastSigninTime = lastSigninTime;
	}
	
	@Column(name="LOGIN_IP")
	public String getLoginIp() {
        return loginIp;
    }
    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @Column(name = "LAST_SIGNIN_IP", length = 100)
	public String getLastSigninIp() {
		return this.lastSigninIp;
	}

	public void setLastSigninIp(String lastSigninIp) {
		this.lastSigninIp = lastSigninIp;
	}

	@Column(name = "MODIFIED_TIME")
	public Date getModifiedTime() {
		return this.modifiedTime;
	}

	public void setModifiedTime(Date modifiedTime) {
		this.modifiedTime = modifiedTime;
	}

	@Column(name = "RESET_TIME")
	public Date getResetTime() {
		return resetTime;
	}

	public void setResetTime(Date resetTime) {
		this.resetTime = resetTime;
	}

	@Column(name = "RESET_TOKEN")
	public String getResetToken() {
		return resetToken;
	}

	public void setResetToken(String resetToken) {
		this.resetToken = resetToken;
	}

	@Column(name = "EMAIL")
	public String getEmail() {
		return email;
	}
    public void setEmail(String email) {
		this.email = email;
	}
    @Column(name = "MOBILE")
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    
	@Column(name = "MODIFIED_ID", precision = 10, scale = 0)
	public Long getModifiedId() {
		return this.modifiedId;
	}

	public void setModifiedId(Long modifiedId) {
		this.modifiedId = modifiedId;
	}

	@Column(name = "DISABLED", precision = 1, scale = 0)
	public Boolean getDisabled() {
		return this.disabled;
	}
	public void setDisabled(Boolean disabled) {
		this.disabled = disabled;
	}
	
	@Column(name = "IS_SYSTEM", nullable = false, precision = 1, scale = 0)
	public Boolean getIsSystem() {
        return isSystem;
    }

    public void setIsSystem(Boolean isSystem) {
        this.isSystem = isSystem;
    }
    
    @Column(name = "COUNT_LOGIN", precision = 10, scale = 0)
    public Integer getCountLogin() {
        return countLogin;
    }
    public void setCountLogin(Integer countLogin) {
        this.countLogin = countLogin;
    }

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "account")
	public Set<AccountRole> getAccountRoles() {
		return this.accountRoles;
	}

	public void setAccountRoles(Set<AccountRole> accountRoles) {
		this.accountRoles = accountRoles;
	}

	
	public Account() {
	}

	public Account(Long aid) {
		this.aid = aid;
	}

	@Column(name = "NUMBERS")
	public String getNumbers() {
		return numbers;
	}

	public void setNumbers(String numbers) {
		this.numbers = numbers;
	}
	
	@Column(name = "DUTY_CODE" , length = 10, nullable = true)
	public String getDutyCode() {
		return dutyCode;
	}

	public void setDutyCode(String dutyCode) {
		this.dutyCode = dutyCode;
	}	
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
}
